package com.yzp.zk_lock;

/**
 * @title: OrderService
 * @Author: yzp
 * @Date: 2019/7/7 10:04
 * @Version 1.0
 */
public class OrderService implements Runnable {
    private OrderNumGenerator orderNumGenerator = new OrderNumGenerator();

    private ExtLock lock = new ZookeeperDistrbuteLock();

    @Override
    public void run() {
        getNumber();
    }

    private void getNumber() {
        try {
            //获取锁
            lock.getLock();
            String number = orderNumGenerator.getNumber();
            System.out.println(Thread.currentThread().getName() + "订单ID：" + number);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            lock.unLock();
        }
    }

    public static void main(String[] args) {
        System.out.println("####生成唯一订单号###");
//		OrderService orderService = new OrderService();
        for (int i = 0; i < 100; i++) {
            new Thread( new OrderService()).start();
        }
    }
}
